package com.binvshe.binvshe.organization;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;

import com.binvshe.binvshe.R;
import com.binvshe.binvshe.common.http.ListDataRequestListener;
import com.binvshe.binvshe.common.http.MyHttpRequest;
import com.binvshe.binvshe.common.http.UrlCenter;
import com.binvshe.binvshe.organization.entity.OrgTypeEntity;
import com.binvshe.binvshe.organization.tag.TagBean;
import com.pachong.android.baseuicomponent.adapter.RecyclerViewBaseAdapter;
import com.pachong.android.baseuicomponent.fragment.SwipeListFragment;
import com.pachong.android.framework.httprequest.IHttpRequest;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Zoi.
 * E-mail：KyluZoi@gmail.com
 * 2016/9/18
 */
public class SearchTypeFragment extends SwipeListFragment {
    private int mPage = 0;

    public static SearchTypeFragment newInstance() {
        Bundle args = new Bundle();
        SearchTypeFragment fragment = new SearchTypeFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onResume() {
        super.onResume();
        startLoading();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        clearDecorations();
        setPullToRefreshEnable(false);
        return view;
    }

    @Override
    public RecyclerViewBaseAdapter createAdapter() {
        return new SearchTypeAdapter(getContext());
    }

    @Override
    public RecyclerView.LayoutManager createLayoutManager() {
        return new GridLayoutManager(getContext(), 3, LinearLayoutManager.VERTICAL, false);
    }

    @Override
    public void onStartLoading() {
        if (mResponceListener.getResponseDataParser().hasMore()) {
            mPage++;
        }
        getData().clear();
        getOrgType();
        getAdapter().notifyDataSetChanged();
    }

    private ListDataRequestListener<OrgTypeEntity> mResponceListener = new ListDataRequestListener(this, OrgTypeEntity.class);

    /**
     * 获取可选的类型
     */
    private void getOrgType() {
        IHttpRequest request = new MyHttpRequest(getContext());
        request.get(UrlCenter.GET_ORG_TYPE, null, mResponceListener);
    }

    protected class SearchTypeAdapter extends RecyclerViewBaseAdapter<OrgTypeEntity> {

        public SearchTypeAdapter(Context con) {
            super(con);
        }

        @Override
        public RecyclerView.ViewHolder onCreateHolder(ViewGroup parent, int viewType) {
            View view = ((LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
                    R.layout.item_type_tag, parent, false);
            return new VH(view);
        }

        @Override
        public void onBindHolder(RecyclerView.ViewHolder holder, int position) {
            VH mholder = (VH) holder;
            OrgTypeEntity data = getData().get(position);
            TagBean bean = new TagBean();
            bean.setPositon(position);
            mholder.tvTag.setText(data.getTypeName());
            mholder.tvTag.setTextColor(bean.getTagTextColor());
            bean.getTagBackGround(mholder.tvTag);
            setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    int typeID=((OrgTypeEntity)getAdapter().getData().get(position)).getId();
                    ClubSearchResultActivity.start(
                            getContext(), ClubSearchResultActivity.USE_TYPEID, typeID
                    );
                }
            });
            setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                }
            });
        }

        protected class VH extends RecyclerView.ViewHolder {
            @Bind(R.id.tvTag)
            TextView tvTag;

            public VH(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }
    }
}
